<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta name="referrer" content="no-referrer"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>010.C++核心编程-内存分区模型 | 扣小妖</title><meta name="author" content="扣小妖"><meta name="copyright" content="扣小妖"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="本阶段主要针对C++面向对象编程技术做详细讲解，探讨C++中核心和精髓~~~  在线课程 https:&#x2F;&#x2F;www.bilibili.com&#x2F;video&#x2F;BV1et411b73Z?p&#x3D;84       div .img_left{         float:left;     }   C++程序在执行时，将内存大方向划分为4个区域  代码区：存放函数体的二进制代码，由操作系统进行管理的 全局区：存">
<meta property="og:type" content="article">
<meta property="og:title" content="010.C++核心编程-内存分区模型">
<meta property="og:url" content="http://liukuaizhuan.gitee.io/posts/9368cbfc.html">
<meta property="og:site_name" content="扣小妖">
<meta property="og:description" content="本阶段主要针对C++面向对象编程技术做详细讲解，探讨C++中核心和精髓~~~  在线课程 https:&#x2F;&#x2F;www.bilibili.com&#x2F;video&#x2F;BV1et411b73Z?p&#x3D;84       div .img_left{         float:left;     }   C++程序在执行时，将内存大方向划分为4个区域  代码区：存放函数体的二进制代码，由操作系统进行管理的 全局区：存">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://p.ananas.chaoxing.com/star3/origin/b4c7f6d417c4c75ed02262f3549625c8.png">
<meta property="article:published_time" content="2023-02-06T15:28:05.000Z">
<meta property="article:modified_time" content="2023-02-13T15:33:58.476Z">
<meta property="article:author" content="扣小妖">
<meta property="article:tag" content="C++核心编程">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p.ananas.chaoxing.com/star3/origin/b4c7f6d417c4c75ed02262f3549625c8.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://liukuaizhuan.gitee.io/posts/9368cbfc"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://gcore.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://gcore.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://gcore.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '010.C++核心编程-内存分区模型',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-02-13 23:33:58'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://p.ananas.chaoxing.com/star3/origin/c6eb667caaea4b46a934d65b6a348821.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://p.ananas.chaoxing.com/star3/origin/b4c7f6d417c4c75ed02262f3549625c8.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">扣小妖</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">010.C++核心编程-内存分区模型</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2023-02-06T15:28:05.000Z" title="发表于 2023-02-06 23:28:05">2023-02-06</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/C-%E6%A0%B8%E5%BF%83%E7%BC%96%E7%A8%8B/">C++核心编程</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="010.C++核心编程-内存分区模型"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>本阶段主要针对C++面向对象编程技术做详细讲解，探讨C++中核心和精髓~~~</p>
<blockquote>
<p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=84">https://www.bilibili.com/video/BV1et411b73Z?p=84</a> </p>
</blockquote>
<style>
   div .img_left{
        float:left;
    }
</style>

<p>C++程序在执行时，将内存大方向划分为4个区域</p>
<ul>
<li>代码区：存放函数体的二进制代码，由操作系统进行管理的</li>
<li>全局区：存放全局变量和静态变量以及常量</li>
<li>栈区：由编辑器自动分配释放，存放函数的参数值，局部变量等</li>
<li>堆区：由程序员分配和释放，若程序员不释放，程序结束时由操作系统回收</li>
</ul>
<h3 id="内存四分区模型"><a href="#内存四分区模型" class="headerlink" title="内存四分区模型"></a><strong>内存四分区模型</strong></h3><img src="https://p.ananas.chaoxing.com/star3/origin/af29402e232ccef5c8caf0582964f871.png" class="" width="450" height="440">

<p><strong>内存四区意义：</strong></p>
<p>不同区域存放的数据，赋予不同的生命周期，给我们更大的灵活编程</p>
<hr>
<h3 id="程序运行前"><a href="#程序运行前" class="headerlink" title="程序运行前"></a>程序运行前</h3><p>在程序编译后，生成了exe可执行程序，未执行该程序前分为两个区域 (代码区和全局区)</p>
<hr>
<h4 id="代码区"><a href="#代码区" class="headerlink" title="代码区"></a><strong>代码区</strong></h4><ul>
<li>存放CPU执行机器指令</li>
<li>代码区是<mark class="hl-label red">共享</mark>  的，共享的目的是对于频繁被执行的程序，只需要再内存中有一份代码即可</li>
<li>代码区是<mark class="hl-label red">只读</mark>  的，使其只读的原因是防止程序意外地修改了它的指令</li>
</ul>
<hr>
<h4 id="全局区"><a href="#全局区" class="headerlink" title="全局区"></a><strong>全局区</strong></h4><blockquote>
<p>在线课程  <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z/?p=85">https://www.bilibili.com/video/BV1et411b73Z/?p=85</a></p>
</blockquote>
<ul>
<li>全局变量和静态变量存放在此。</li>
<li>全局区还包含了常量区，字符串常量和其他常量也存放在此。</li>
<li><mark class="hl-label green">该区域的数据在程序结束后由操作系统释放。</mark> </li>
</ul>
<p><strong>哪些在全局区，哪些不在全局区汇总：</strong></p>
<img src="https://p.ananas.chaoxing.com/star3/origin/aeae60fe76f19df6316bff1b7efc776e.png" class="">

<p><strong>执行代码测试内存地址：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac操作系统，开发环境 Clion , on 2023/2/7.</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">// 全局变量</span></span><br><span class="line"><span class="type">int</span> g_a = <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> g_b = <span class="number">10</span>;</span><br><span class="line"><span class="comment">// const 修饰的全局变量，全局常量</span></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> c_g_a = <span class="number">10</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> c_g_b = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123; </span><br><span class="line">    <span class="comment">// 全局区中存放的数据：全局变量，静态变量，常量</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建普通局部变量</span></span><br><span class="line">    <span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> b = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    cout&lt;&lt; <span class="string">&quot;局部变量a的地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;a &lt;&lt;endl; <span class="comment">// 局部变量 不是存放在全局区中</span></span><br><span class="line">    cout&lt;&lt; <span class="string">&quot;局部变量b的地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;b &lt;&lt;endl; <span class="comment">// 局部变量 不是存放在全局区中</span></span><br><span class="line"></span><br><span class="line">    cout&lt;&lt; <span class="string">&quot;全局变量g_a的地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;g_a &lt;&lt;endl; <span class="comment">//存放在全局区中</span></span><br><span class="line">    cout&lt;&lt; <span class="string">&quot;全局变量g_b的地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;g_b &lt;&lt;endl; <span class="comment">//存放在全局区中</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 静态变量 在普通变量前加static ，属于静态变量</span></span><br><span class="line">    <span class="type">static</span> <span class="type">int</span> s_a = <span class="number">10</span>;</span><br><span class="line">    <span class="type">static</span> <span class="type">int</span> s_b = <span class="number">10</span>;</span><br><span class="line">    cout&lt;&lt; <span class="string">&quot;静态变量s_a的地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;s_a &lt;&lt;endl; <span class="comment">//存放在全局区中</span></span><br><span class="line">    cout&lt;&lt; <span class="string">&quot;静态变量s_b的地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;s_b &lt;&lt;endl; <span class="comment">//存放在全局区中</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 常量</span></span><br><span class="line">    <span class="comment">// 字符串常量</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;字符串常量的地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;<span class="string">&quot;hello word&quot;</span>&lt;&lt;endl; <span class="comment">//存放在全局区中</span></span><br><span class="line">    <span class="comment">// const修饰的变量</span></span><br><span class="line">    <span class="comment">// const修饰的全局变量，const修饰的局部变量</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;全局常量c_g_a 地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;c_g_a&lt;&lt;endl; <span class="comment">//存放在全局区中</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;全局常量c_g_b 地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;c_g_b&lt;&lt;endl; <span class="comment">//存放在全局区中</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//const修饰的局部变量</span></span><br><span class="line">    <span class="type">const</span> <span class="type">int</span> c_l_a = <span class="number">10</span>;</span><br><span class="line">    <span class="type">const</span> <span class="type">int</span> c_l_b = <span class="number">10</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;局部常量c_l_a 地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;c_l_a&lt;&lt;endl;  <span class="comment">// 局部变量 不是存放在全局区中</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;局部常量c_l_b 地址为：&quot;</span>&lt;&lt; (<span class="type">long</span>)&amp;c_l_b&lt;&lt;endl;  <span class="comment">// 局部变量 不是存放在全局区中</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>代码执行后显示的内存地址效果：</strong></p>
<img src="https://p.ananas.chaoxing.com/star3/origin/ab24024eb5f6075b3715fb7ffa6bd994.png" class="img_left">

<p><strong>总结：</strong></p>
<div class="note success no-icon flat"><ul>
<li>C++中在程序运行前分为 <mark class="hl-label pink">全局区</mark>  和  <mark class="hl-label pink">代码区</mark> </li>
<li>代码区的特点是<mark class="hl-label purple">共享</mark>  和 <mark class="hl-label purple">只读</mark> </li>
<li>全局区中存放 <mark class="hl-label orange">全局变量、静态变量、常量</mark> </li>
<li>常量区中存放 <mark class="hl-label blue">const修饰的全局常量</mark>  和 <mark class="hl-label blue">字符串常量</mark> </li>
</ul>
</div>


<hr>
<h3 id="程序运行后"><a href="#程序运行后" class="headerlink" title="程序运行后"></a>程序运行后</h3><blockquote>
<p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z/?p=86">https://www.bilibili.com/video/BV1et411b73Z/?p=86</a> </p>
</blockquote>
<h4 id="栈区"><a href="#栈区" class="headerlink" title="栈区"></a><strong>栈区</strong></h4><p>由编译器自动分配释放，存放函数的参数值，局部变量等<br>注意事项：不要返回局部变量的地址，栈区开辟的数据由编译器自动释放</p>
<p><strong>示例：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac操作系统，开发环境 Clion , on 2023/2/7.</span></span><br><span class="line"><span class="comment">// 栈区数据的注意事项</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> * <span class="title">func</span><span class="params">(<span class="type">int</span> b)</span> </span>&#123; <span class="comment">// 形参数据也会放在栈区</span></span><br><span class="line">    b = <span class="number">100</span>;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">10</span>; <span class="comment">// 局部变量,存放在栈区，栈区的数据在函数执行完后自动释放</span></span><br><span class="line">    <span class="keyword">return</span> &amp;a; <span class="comment">//返回局部变量的地址</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 栈区数据的注意事项 -- 不要返回局部变量的地址</span></span><br><span class="line">    <span class="comment">// 栈区的数据有编译器管理开辟和释放</span></span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> *p = <span class="built_in">func</span>(<span class="number">100</span>);<span class="comment">//接收func函数的返回值</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;  <span class="comment">// 第一次可以打印正确的数字，是因为编译器做了保留</span></span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;  <span class="comment">// 第二次这个数据就不在保留了，返回的就是乱码了</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h4 id="堆区"><a href="#堆区" class="headerlink" title="堆区"></a>堆区</h4><blockquote>
<p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z/?p=87">https://www.bilibili.com/video/BV1et411b73Z/?p=87</a> </p>
</blockquote>
<p>由程序员分配释放，若程序员不释放，程序结束时由操作系统回收<br>在C++中主要利用<mark class="hl-label red">new</mark> 在堆区开辟内存</p>
<p><strong>示例：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac操作系统  , on 2023/2/7.</span></span><br><span class="line"><span class="comment">// 利用new关键字 ，可以将数据开辟到堆区</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> * <span class="title">func</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 利用new关键字 ，可以将数据开辟到堆区</span></span><br><span class="line">    <span class="comment">// 指针 本质也是局部变量，放在栈上，指针保存的数据是放在堆区</span></span><br><span class="line">    <span class="type">int</span> * p = <span class="keyword">new</span> <span class="built_in">int</span>(<span class="number">10</span>); <span class="comment">// 格式 new 数据类型(赋值的初始值) 。  返回开辟的内存地址</span></span><br><span class="line">    <span class="keyword">return</span> p;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 在堆区开辟数据</span></span><br><span class="line">    <span class="type">int</span>  *p = <span class="built_in">func</span>();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 堆区数据需要程序员释放</span></span><br><span class="line">    cout &lt;&lt;  *p &lt;&lt;endl;</span><br><span class="line">    cout &lt;&lt;  *p &lt;&lt;endl;</span><br><span class="line">    cout &lt;&lt;  *p &lt;&lt;endl;</span><br><span class="line">    cout &lt;&lt;  *p &lt;&lt;endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>总结：</strong></p>
<ul>
<li>堆区数据由程序员管理开辟和释放</li>
<li>堆区数据利用 new 关键字进行开辟内存</li>
</ul>
<hr>
<h3 id="new-操作符"><a href="#new-操作符" class="headerlink" title="new 操作符"></a>new 操作符</h3><blockquote>
<p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z/?p=88">https://www.bilibili.com/video/BV1et411b73Z/?p=88</a> </p>
</blockquote>
<p>C++中利用new操作符在堆区开辟数据</p>
<p>堆区开辟的数据，由程序员手动开辟，手动释放，释放利用操作符 <mark class="hl-label red">delete</mark>   </p>
<p>语法：<code>new 数据类型</code></p>
<p>利用new创建的数据，会返回改数据对应的类型的指针</p>
<p><strong>示例1：基本语法</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac操作系统 , on 2023/2/8. </span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">//1. new的基本语法</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> *<span class="title">func01</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">//在堆区创建整型数据</span></span><br><span class="line">    <span class="comment">// new 返回的是 该数据类型的指针</span></span><br><span class="line">    <span class="type">int</span> *p = <span class="keyword">new</span> <span class="built_in">int</span>(<span class="number">10</span>);</span><br><span class="line">    <span class="keyword">return</span> p;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">test_01</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> *p = <span class="built_in">func01</span>();</span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;</span><br><span class="line">    <span class="comment">// 堆区的数据 由程序员管理开辟，程序员管理释放</span></span><br><span class="line">    <span class="comment">// 如果想释放堆区的数据，利用关键字delete</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">delete</span> p;</span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl; <span class="comment">//内存已经释放，再次访问就是非法操作，会报错</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">test_01</span>(); </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>示例2：开辟数组</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac操作系统 , on 2023/2/8. </span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">// 2. 在堆区利用new开辟数据</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">test_02</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">//创建10整型数据的数组，在堆区</span></span><br><span class="line">    <span class="type">int</span> *arr = <span class="keyword">new</span> <span class="type">int</span>[<span class="number">10</span>]; <span class="comment">// 这个10 代表数组有10个元素</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//给10个元素赋值 100-109</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; ++i) &#123;</span><br><span class="line">        arr[i] = i + <span class="number">100</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 打印操作</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; ++i) &#123;</span><br><span class="line">        cout &lt;&lt; arr[i] &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 释放堆区数组</span></span><br><span class="line">    <span class="comment">// 注意：释放数组的时候，要加[]才可以</span></span><br><span class="line">    <span class="keyword">delete</span>[] arr;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123; </span><br><span class="line">    <span class="built_in">test_02</span>();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://liukuaizhuan.gitee.io">扣小妖</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://liukuaizhuan.gitee.io/posts/9368cbfc.html">http://liukuaizhuan.gitee.io/posts/9368cbfc.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本站所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/C-%E6%A0%B8%E5%BF%83%E7%BC%96%E7%A8%8B/">C++核心编程</a></div><div class="post_share"><div class="social-share" data-image="https://p.ananas.chaoxing.com/star3/origin/b4c7f6d417c4c75ed02262f3549625c8.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://lib.baomitu.com/social-share.js/1.0.16/css/share.min.css" media="print" onload="this.media='all'"><script src="https://lib.baomitu.com/social-share.js/1.0.16/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://p.ananas.chaoxing.com/star3/origin/5a01db3c5a0daca9fa2b024736bf66e3.png" target="_blank"><img class="post-qr-code-img" src="https://p.ananas.chaoxing.com/star3/origin/5a01db3c5a0daca9fa2b024736bf66e3.png" alt="微信打赏"/></a><div class="post-qr-code-desc">微信打赏</div></li><li class="reward-item"><a href="https://p.ananas.chaoxing.com/star3/origin/afffb275198370656cb6cef6a1107185.png" target="_blank"><img class="post-qr-code-img" src="https://p.ananas.chaoxing.com/star3/origin/afffb275198370656cb6cef6a1107185.png" alt="支付宝打赏"/></a><div class="post-qr-code-desc">支付宝打赏</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/bff12c11.html"><img class="prev-cover" src="https://p.ananas.chaoxing.com/star3/origin/86403540a7b572fec123890e9a638adc.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">011.C++核心编程-引用</div></div></a></div><div class="next-post pull-right"><a href="/posts/b056b485.html"><img class="next-cover" src="https://p.ananas.chaoxing.com/star3/origin/5cdbcc7b75aea2aa3a8b12cae1439e99.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">009.C++基础实战-通讯录管理系统</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/bff12c11.html" title="011.C++核心编程-引用"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/86403540a7b572fec123890e9a638adc.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-13</div><div class="title">011.C++核心编程-引用</div></div></a></div><div><a href="/posts/4adebd64.html" title="012.C++核心编程-函数提高"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/b6c49bb4892ad191edbc0363caf45530.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-13</div><div class="title">012.C++核心编程-函数提高</div></div></a></div><div><a href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/7ea674ffcd12ef7d943d54c93e9203a4.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-13</div><div class="title">013.c++核心编程-类和对象</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://p.ananas.chaoxing.com/star3/origin/c6eb667caaea4b46a934d65b6a348821.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">扣小妖</div><div class="author-info__description">Email：  liukuaizhuan@qq.com</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog 技术笔记</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E5%AD%98%E5%9B%9B%E5%88%86%E5%8C%BA%E6%A8%A1%E5%9E%8B"><span class="toc-number">1.</span> <span class="toc-text">内存四分区模型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A8%8B%E5%BA%8F%E8%BF%90%E8%A1%8C%E5%89%8D"><span class="toc-number">2.</span> <span class="toc-text">程序运行前</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E5%8C%BA"><span class="toc-number">2.1.</span> <span class="toc-text">代码区</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E5%8C%BA"><span class="toc-number">2.2.</span> <span class="toc-text">全局区</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A8%8B%E5%BA%8F%E8%BF%90%E8%A1%8C%E5%90%8E"><span class="toc-number">3.</span> <span class="toc-text">程序运行后</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A0%88%E5%8C%BA"><span class="toc-number">3.1.</span> <span class="toc-text">栈区</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A0%86%E5%8C%BA"><span class="toc-number">3.2.</span> <span class="toc-text">堆区</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#new-%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="toc-number">4.</span> <span class="toc-text">new 操作符</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象"><img src="https://p.ananas.chaoxing.com/star3/origin/7ea674ffcd12ef7d943d54c93e9203a4.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="013.c++核心编程-类和对象"/></a><div class="content"><a class="title" href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象">013.c++核心编程-类和对象</a><time datetime="2023-02-13T14:55:03.000Z" title="发表于 2023-02-13 22:55:03">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/4adebd64.html" title="012.C++核心编程-函数提高"><img src="https://p.ananas.chaoxing.com/star3/origin/b6c49bb4892ad191edbc0363caf45530.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="012.C++核心编程-函数提高"/></a><div class="content"><a class="title" href="/posts/4adebd64.html" title="012.C++核心编程-函数提高">012.C++核心编程-函数提高</a><time datetime="2023-02-13T14:51:24.000Z" title="发表于 2023-02-13 22:51:24">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/bff12c11.html" title="011.C++核心编程-引用"><img src="https://p.ananas.chaoxing.com/star3/origin/86403540a7b572fec123890e9a638adc.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="011.C++核心编程-引用"/></a><div class="content"><a class="title" href="/posts/bff12c11.html" title="011.C++核心编程-引用">011.C++核心编程-引用</a><time datetime="2023-02-13T14:47:34.000Z" title="发表于 2023-02-13 22:47:34">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9368cbfc.html" title="010.C++核心编程-内存分区模型"><img src="https://p.ananas.chaoxing.com/star3/origin/b4c7f6d417c4c75ed02262f3549625c8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="010.C++核心编程-内存分区模型"/></a><div class="content"><a class="title" href="/posts/9368cbfc.html" title="010.C++核心编程-内存分区模型">010.C++核心编程-内存分区模型</a><time datetime="2023-02-06T15:28:05.000Z" title="发表于 2023-02-06 23:28:05">2023-02-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/b056b485.html" title="009.C++基础实战-通讯录管理系统"><img src="https://p.ananas.chaoxing.com/star3/origin/5cdbcc7b75aea2aa3a8b12cae1439e99.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="009.C++基础实战-通讯录管理系统"/></a><div class="content"><a class="title" href="/posts/b056b485.html" title="009.C++基础实战-通讯录管理系统">009.C++基础实战-通讯录管理系统</a><time datetime="2023-02-04T10:02:31.000Z" title="发表于 2023-02-04 18:02:31">2023-02-04</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 扣小妖</div><div class="footer_custom_text"><p style="margin:10px 0 5px"><a target="_blank" href="https://hexo.io/" style="margin-inline:5px">博客 hexo</a><a target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px">主题 Butterfly</a><a target="_blank" href="https://gitee.com/" style="margin-inline:5px">托管 Gitee</a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://gcore.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://gcore.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script></div><script defer="defer" id="ribbon" src="https://gcore.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>